package com.cqc.parallxlistview01.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageView;
import android.widget.ListView;

import com.cqc.parallxlistview01.util.LogUtil;

/**
 * Created by a on 2018/3/27.
 *
 */

public class ParallaxListView extends ListView {

    private ImageView ivHeader;

    private int originHeaderHeight;

    public ParallaxListView(Context context) {
        this(context, null);
    }

    public ParallaxListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public ParallaxListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 划出边界的时候调用,就是划出了ListView的高度范围[0,height]
     *
     * @param deltaX
     * @param deltaY
     * @param scrollX
     * @param scrollY
     * @param scrollRangeX
     * @param scrollRangeY
     * @param maxOverScrollX
     * @param maxOverScrollY
     * @param isTouchEvent
     * @return
     */
    @Override
    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
//        LogUtil.d("deltaX=" + deltaX + ",deltaY=" + deltaY + ",scrollX=" + scrollX + ",scrollY=" + scrollY + ",scrollRangeX=" + scrollRangeX + ",scrollRangeY=" + scrollRangeY + ",isTouchEvent=" + isTouchEvent);
        if (deltaY < 0 && isTouchEvent) {
            int topHeight = ivHeader.getHeight() - deltaY / 2;
            setHeaderHeight(topHeight);
        }
        return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
    }


    /**
     * 手指已开后 在属性动画中获取值，改变headerImageView的高度
     * @param ev
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            int currentHeight = ivHeader.getHeight();
            ValueAnimator valueAnimator = ValueAnimator.ofInt(currentHeight, originHeaderHeight);
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    int height = (int) animation.getAnimatedValue();
                    setHeaderHeight(height);
                }
            });
            valueAnimator.setInterpolator(new OvershootInterpolator());
            valueAnimator.setDuration(currentHeight - originHeaderHeight);
            valueAnimator.start();
        }
        return super.onTouchEvent(ev);
    }

    public void setHeaderImageView(ImageView ivHeader) {
        this.ivHeader = ivHeader;
        originHeaderHeight = ivHeader.getHeight();
    }

    private void setHeaderHeight(int newHeight) {
        ivHeader.getLayoutParams().height = newHeight;
        ivHeader.requestLayout();
    }


}
